package reaction

import (
	"context"
	// "fmt"
	"strings"

	"mall/common/modelx"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/blog/model"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/types/blog"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetReactionLogListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetReactionLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetReactionLogListLogic {
	return &GetReactionLogListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetReactionLogListLogic) GetReactionLogList(in *blog.BaseListReq) (*blog.ReactionLogListResp, error) {
	if in.Filter == nil || len(*in.Filter) == 0 {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	// preloadMap, eagerLoadMap := modelx.ParsePreloadAndEagerLoad(in.Preload, in.EagerLoad)
	preloadMap, _ := modelx.ParsePreloadAndEagerLoad(in.Preload, in.EagerLoad)

	// logrus.Info(fmt.Sprintf("GetReactionLogList preloadMap: %+v", preloadMap))
	// map[blogReactionLog:[PostId Status Count]]

	// logrus.Info(fmt.Sprintf("GetReactionLogList eagerLoadMap: %+v", eagerLoadMap))
	// map[]

	blogReactionLogEntityName := model.RpcEntityNames.BlogReactionLog
	if _, ok := model.RpcEntityPreloadMap[blogReactionLogEntityName]; !ok {
		return &blog.ReactionLogListResp{}, nil
	}

	blogReactionLogRepositoryName := model.RepositoryNames.BlogReactionLog
	columnMap, ok := model.RepositoryPreloadMap[blogReactionLogRepositoryName]
	if !ok {
		return &blog.ReactionLogListResp{}, nil
	}

	var selectCols []string
	if preloadFields, ok := preloadMap[blogReactionLogEntityName]; ok {
		selectCols = modelx.ParseSelectColumns(preloadFields, columnMap)
	}

	filterMap := modelx.ParseFilter(in.Filter, columnMap)
	// logrus.Info(fmt.Sprintf("GetReactionLogList filterMap: %+v", filterMap))
	// map[post_id:map[equalTo:[40054]] post_type:map[equalTo:[141]] type:map[equalTo:[want]] user_id:map[equalTo:[10021]]]
	if filterMap == nil {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	orderBy := modelx.ParseSorter(in.Sorter, columnMap)
	// logrus.Info(fmt.Sprintf("GetReactionLogList orderBy: %s", orderBy))
	//
	if len(orderBy) == 0 {
		orderBy = model.BlogReactionLogColumns.ID + " DESC"
	}

	res, _, err := l.svcCtx.Reaction.GetList(l.ctx, model.M{
		"select":  strings.Join(selectCols, ","),
		"latest":  "1",
		"orderBy": orderBy,
	}, -1, filterMap)
	if err != nil {
		return nil, err
	}

	resp := &blog.ReactionLogListResp{}
	if len(res) > 0 {
		for _, v := range res {
			resp.List = append(resp.List, &blog.ReactionLogInfo{
				Id:         &v.ID,
				PostId:     &v.PostID,
				PostType:   pointy.GetPointer(uint32(v.PostType)),
				UserId:     &v.UserID,
				Type:       &v.Type,
				Status:     pointy.GetPointer(uint32(v.Status)),
				Count:      &v.Count,
				CreatedAt:  pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
				CreateDate: pointy.GetPointer(uint32(v.CreateDate)),
			})
		}
	}

	if in.PreFilter != nil {
		preFilterMap := modelx.ParseFilter(in.PreFilter, columnMap)
		// logrus.Info(fmt.Sprintf("GetReactionLogList preFilterMap: %+v", preFilterMap))
		// map[post_id:map[equalTo:[40054]] post_type:map[equalTo:[141]] type:map[equalTo:[want]]]
		if preFilterMap != nil {
			preOrderBy := modelx.ParseSorter(in.PreSorter, columnMap)
			// logrus.Info(fmt.Sprintf("GetReactionLogList preOrderBy: %s", preOrderBy))
			//
			if len(preOrderBy) == 0 {
				preOrderBy = model.BlogReactionLogColumns.ID + " DESC"
			}

			list, _, err := l.svcCtx.Reaction.GetList(l.ctx, model.M{
				"select":  strings.Join(selectCols, ","),
				"latest":  "1",
				"orderBy": preOrderBy,
			}, -1, preFilterMap)
			if err != nil {
				return nil, err
			}

			if len(list) > 0 {
				for _, v := range list {
					resp.Extra = append(resp.Extra, &blog.ReactionLogInfo{
						Id:         &v.ID,
						PostId:     &v.PostID,
						PostType:   pointy.GetPointer(uint32(v.PostType)),
						UserId:     &v.UserID,
						Type:       &v.Type,
						Status:     pointy.GetPointer(uint32(v.Status)),
						Count:      &v.Count,
						CreatedAt:  pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
						CreateDate: pointy.GetPointer(uint32(v.CreateDate)),
					})
				}
			}
		}
	}

	return resp, nil
}
